//index.js
const app = getApp()
const util = require('../../utils/util.js')
const lbbService = require('../service/service.js')

Page({
  data: {
    accountPhone: '',
    verCode: '',
    disabled: false,
    loginDisabled: false,
    codename: '发送验证码',
    timer:null,
    step:1,
  },

  onLoad: function() {
    this.wxAutoLogin()
  },
  onShow: function() {

  },
  onUnload: function(){
    // wx.removeStorageSync('accountPhone')
    wx.removeStorageSync('verCode')
  },
  onReady: function(){
  },
  toStep(){
    this.setData({
      step:2
    })
  },
  getVerificationCode: function () {
    var that = this
    if (that.checkPhone(that.data.accountPhone)){
      that.getVerCode();
      that.setData({
        disabled: true
      })
    }
  },
  getVerCode: function () {
    var that = this;
    var num = 59;
    var timer = setInterval(function () {
      num--;
      if (num <= 0) {
        clearInterval(timer);
        that.setData({
          codename: '重新发送',
          disabled: false
        })
      } else {
        that.setData({
          codename: num + "s"
        })
      }
    }, 1000)
    that.setData({
      timer:timer
    })
    wx.request({
      url: util.CONST_DATA.BASE_URL + 'ulogin/getpcode',
      header: app.globalData.header,
      method: 'post',
      data: { phone: that.data.accountPhone },
      success: function (res) {
        console.log('login',res)
        if (res.data.result == 0) {
          wx.showToast({
            title: '已发送',
          })
        } else {
          wx.showToast({
            title: '发送失败',
            image: '/images/gantan.png'
          })
        }
      },
      fail: function (res) {
        console.log('login', res)
        wx.showToast({
          title: '发送fail',
          image: '/images/gantan.png'
        })
      }
    })
  },
  /**
   * 自动登录
   */
  wxAutoLogin:function() {
    wx.showLoading({
      title: '登录中...',
    })
    let that = this
    wx.login({
      success(res) {
        that.setData({
          loginDisabled: false
        })
        wx.request({
          url: util.CONST_DATA.BASE_URL + 'ulogin/wxAutoLogin',
          header: app.globalData.header,
          method: 'post',
          data: {
            code: res.code, //小程序登录获取到的code 字符串类型
          },
          success: function (res) {
            wx.hideLoading({
              success: (res) => {},
            });
            if (res.data.result != 0) {
              wx.showToast({
                title: res.data.description,
                image: '/images/gantan.png'
              })
            } else {
              util.saveToken(res.data.data.token, res.data.data.pass, res.data.data.ltime);
              app.globalData.header.token = res.data.data.token;
              wx.switchTab({
                url: '/pages/hospitalList/hospitalList',
              })
            }
          },
          fail: function (res) {
            wx.hideLoading({
              success: (res) => {},
            });
            wx.showToast({
              title: '自动登录失败',
              icon: 'none'
            })
          }
        })
      },
      fail(err) {
        wx.hideLoading({
          success: (res) => {},
        });
        wx.showToast({
          title: '自动登录失败',
          icon: 'none'
        })
      }
    })
  },
  formSubmit: function(e) {
    let that = this
    let accountPhone = that.data.accountPhone
    let verCode = e.detail.value.verCode
    if (!that.checkPhone(accountPhone)) {
      return
    }
    that.setData({
      loginDisabled:true
    })
    console.log('发送登陆请求')
    wx.login({
      success(res) {
        that.setData({
          loginDisabled: false
        })
        wx.request({
          url: util.CONST_DATA.BASE_URL + 'ulogin/login',
          header: app.globalData.header,
          method: 'post',
          data: {
            // code: app.globalData.js_code, 
            code: res.code, //小程序登录获取到的code 字符串类型
            phone: accountPhone, //手机号 字符串类型
            pcode: verCode,// 手机短信码 字符串类型
          },
          success: function (res) {
            if (res.data.result != 0) {
              wx.showToast({
                title: res.data.description,
                image: '/images/gantan.png'
              })
            } else {
              clearInterval(that.data.timer);
              util.saveToken(res.data.data.token, res.data.data.pass, res.data.data.ltime);
              app.globalData.header.token = res.data.data.token;
              wx.switchTab({
                url: '/pages/hospitalList/hospitalList',
              })
            }
          },
          fail: function (res) {
            wx.showToast({
              title: '登录失败',
              icon: 'none'
            })
          }
        })
      },
      fail(err) {
        wx.showToast({
          title: '登录失败',
          icon: 'none'
        })
        that.setData({
          loginDisabled: false
        })
      }
    })
  },
  blurEventA: function(e) {
    let that = this
    console.log(e)
    let accountPhone = e.detail.value
    wx.setStorageSync('accountPhone', accountPhone)
    that.setData({
      accountPhone: e.detail.value
    })
  },
  blurEventP: function (e) {
    let that = this
    console.log(e)
    let verCode = e.detail.value
    wx.setStorageSync('verCode', verCode)
    that.setData({
      verCode
    })
  },
  checkPhone: function(accountPhone) {
    let that = this
    if (accountPhone=='12345678901'){
      return true
    }
    let myreg = /^1[3456789]{1}\d{9}$/;
    if (!myreg.test(accountPhone)) {
      wx.showToast({
        title: '无效的手机号码',
        image:'/images/gantan.png'
      })
      return false;
    }
    return true
  }
})